﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formProductWriteoff : FormMDIChildTemplate 
    {
        public formProductWriteoff()
        {
            InitializeComponent();
        }

        private void formMaterialProductWriteoff_Load(object sender, EventArgs e)
        {
            FillInvoice();
            root.TranslateGrid(gridInvoice);
            var storeHouses = context.StoreHouses.Select(x => x);
            storeHousebindingSource.DataSource = storeHouses;
        }

        private void FillInvoice()
        {
            var invoiceWritteOff = context.InvoiceWriteoffs.OrderByDescending(x => x.DateCreate);
            invoiceWriteOffbindingSource.DataSource = invoiceWritteOff;
            
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (gridInvoice.Rows.Count > 0)
            {
                InvoiceWriteoff invoiceWriteoff = (InvoiceWriteoff)invoiceWriteOffbindingSource.Current;
                var formProductWriteoffDetail = new formProductWriteoffDetail(invoiceWriteoff);

                if (invoiceWriteoff.InvoiceOutcomes.Count  != 0 || invoiceWriteoff.InvoiceRelocation != null)
                {
                    formProductWriteoffDetail.splitContainer1.Panel1Collapsed = true;
                    formProductWriteoffDetail.buttonRemove.Visible = false;
                }
                root.ShowForm(formProductWriteoffDetail);
            }
        }

        private void invoiceWriteOffbindingSource_CurrentChanged(object sender, EventArgs e)
        {
            InvoiceWriteoff invoiceWriteoff = (InvoiceWriteoff)invoiceWriteOffbindingSource.Current;
            if (invoiceWriteoff.InvoiceRelocation == null && invoiceWriteoff.InvoiceOutcomes.Count == 0)
            {gridInvoice.ReadOnly = false; bindingNavigatorDeleteItem.Enabled = true; }
            else { gridInvoice.ReadOnly = true; bindingNavigatorDeleteItem.Enabled = false; }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            InvoiceWriteoff InvoiceWriteoff = (InvoiceWriteoff)invoiceWriteOffbindingSource.AddNew();
            if (gridInvoice.RowCount > 1)
                InvoiceWriteoff.InvoiceWriteoffNumber =
                    context.InvoiceWriteoffs.Max(x => x.InvoiceWriteoffNumber) + 1;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            InvoiceWriteoff InvoiceWriteoff = (InvoiceWriteoff)invoiceWriteOffbindingSource.Current;
            if (InvoiceWriteoff != null) db.DeleteInvoiceWriteoff(InvoiceWriteoff);
            FillInvoice();
        }

        private void gridInvoice_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            string value = e.Value as string;
            string columnName = gridInvoice.Columns[e.ColumnIndex].Name.ToUpper();
            
            if (columnName.Contains("STOREHOUSE"))
            {
                StoreHouse storeHouse = (from x in storeHousebindingSource.OfType<StoreHouse>()
                                         where x.ToString() == value
                                         select x).FirstOrDefault();
                e.Value = storeHouse;
            }

            e.ParsingApplied = true;
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            ActiveControl = buttonSelect;
            root.Submit();
            FillInvoice();
        }
    }
}
